package cc.rengu.igas.opms.core.realize.impl;

import cc.rengu.igas.opms.common.constant.OpmsParamConstant;
import cc.rengu.igas.opms.common.enums.CardTypeEnum;
import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.common.enums.SignTokenTypeEnum;
import cc.rengu.igas.opms.core.model.SignCardInfoBean;
import cc.rengu.igas.opms.core.realize.SignCardInfoService;
import cc.rengu.igas.share.core.model.BankCodeInfo;
import cc.rengu.igas.share.core.realize.BompService;
import cc.rengu.igas.share.core.realize.impl.BompServiceImpl;
import cc.rengu.oltp.service.common.entity.BinInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.AcctTypeEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.BinInfoService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.BinInfoServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @author wangcheng
 * @version 1.0.0 创建时间：2021/06/05
 */
public class SignCardInfoServiceImpl implements SignCardInfoService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public SignCardInfoBean getSignCardInfo(String instId, String cardNo, String signTokenType) throws Exception {
        BinInfoService binInfoService = new BinInfoServiceImpl();
        BinInfo cardBinInfo = binInfoService.getCardBinInfo(cardNo);
        if (cardBinInfo == null) {
            rglog.error("未查询到对应卡BIN acctNo<{}>", cardNo);
            throw new BizException(RespCodeEnum.NOT_SURP_SIGNTOEKN_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SURP_SIGNTOEKN_TYPE_ERROR.getRespDesc());
        }

        //签约类型为空
        if (StringUtil.isEmptyOrNull(signTokenType)) {
            //查询本行系统参数表中配置的本行卡BIN开头4为配置
            SysParamService sysParamService = new SysParamServiceImpl();
            SysParam sysParam = sysParamService.getSysParamInfo(instId, OpmsParamConstant.OPMS_SYS_PARAM, OpmsParamConstant.CORPORATION_ORG_CODE);
            if (null == sysParam || null == sysParam.getParamStatus()) {
                rglog.info("法人行机构编码未配置或配置失效,instId:<{}>", instId);
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            //判断当前卡是不是本行卡
            if (sysParam.getParamValue().contains(cardBinInfo.getIssuerCode().substring(0, 4))) {
                signTokenType = SignTokenTypeEnum.BANK_QUICK_PAY.getSignTypeCode();
            } else {
                signTokenType = SignTokenTypeEnum.UPAC_TOKEN.getSignTypeCode();
            }
        }
        SignCardInfoBean signCardInfoBean = new SignCardInfoBean();
        if (SignTokenTypeEnum.BANK_QUICK_PAY.getSignTypeCode().equals(signTokenType)) {
            signCardInfoBean.setOwnerBank(true);
            if (CardTypeEnum.DEBIT.getAcctType().equals(cardBinInfo.getCardType()) ||
                    CardTypeEnum.PRECARD.getAcctType().equals(cardBinInfo.getCardType())) {
                signCardInfoBean.setAcctType(AcctTypeEnum.BANK_DEBIT.getAcctType());
            } else {
                signCardInfoBean.setAcctType(AcctTypeEnum.BANK_CREDIT.getAcctType());
            }
        } else {
            signCardInfoBean.setOwnerBank(false);
            if (CardTypeEnum.DEBIT.getAcctType().equals(cardBinInfo.getCardType()) ||
                    CardTypeEnum.PRECARD.getAcctType().equals(cardBinInfo.getCardType())) {
                signCardInfoBean.setAcctType(AcctTypeEnum.OTHER_DEBIT.getAcctType());
            } else {
                signCardInfoBean.setAcctType(AcctTypeEnum.OTHER_CREDIT.getAcctType());
            }
        }

        //查询银联联行号信息，用于签约
        BompService bompService = new BompServiceImpl();
        BankCodeInfo bankCodeInfo = bompService.getBankCodeInfoByCupsInstId(cardBinInfo.getIssuerCode(), cardBinInfo.getCardSign());
        if (null == bankCodeInfo) {
            rglog.error("根据银行机构号获取联行行号失败,cupsInstId:<{}>,cardSign:<{}>", instId, cardBinInfo.getCardSign());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        signCardInfoBean.setBankName(bankCodeInfo.getBankName());
        signCardInfoBean.setBankBranchCode(bankCodeInfo.getBankBranchCode());
        return signCardInfoBean;
    }
}
